package com.spooky.sync.persistence.db;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedList;
import java.util.List;
import com.spooky.sync.persistence.dao.CategoryDAOBean;
import com.spooky.sync.persistence.dao.NoteDAOBean;
import com.spooky.sync.persistence.dao.NoteTagDAOBean;
import com.spooky.sync.persistence.dao.TagDAOBean;
import com.spooky.sync.persistence.entity.BaseEntity;

public class TableCreateSyntax implements SyntaxCreator {
	private static final String TAG = "TableCreateSyntax";
	public static final String DATABASE = "note";
	public static final int DATABASE_VERSION = 9;
	private List<Class<? extends ExtendedDAOBean<?>>> mTableClass = new LinkedList<Class<? extends ExtendedDAOBean<?>>>();

	public TableCreateSyntax() {
		initiate();
	}

	private void initiate() {
		mTableClass.add(TagDAOBean.class);
		mTableClass.add(CategoryDAOBean.class);
		mTableClass.add(NoteDAOBean.class);
		mTableClass.add(NoteTagDAOBean.class);
	}

	@Override
	public String[] create() throws IllegalAccessException, InstantiationException, NullPointerException, IllegalArgumentException, InvocationTargetException {
		StringBuffer str = null;
		String[] buffer = new String[mTableClass.size()];
		Constructor<?> construct = null;
		int i = 0;
		for (Class<? extends ExtendedDAOBean<?>> tableClass : mTableClass) {
			str = new StringBuffer();
			try {
				construct = tableClass.getDeclaredConstructor();
				construct.setAccessible(true);
				ExtendedDAOBean<BaseEntity> obj = ((ExtendedDAOBean<BaseEntity>) construct.newInstance());
				str.append(obj.getCreateSyntax());
			} catch (Exception e) {
				e.printStackTrace();
			}
			buffer[i++] = str.toString();
		}
		return buffer;
	}

	@Override
	public String[] getTables() throws IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
		String[] tables = new String[mTableClass.size()];
		Constructor<?> construct = null;
		int i = 0;
		for (Class<? extends ExtendedDAOBean<?>> tableClass : mTableClass) {
			try {
				construct = tableClass.getDeclaredConstructor();
				construct.setAccessible(true);
				ExtendedDAOBean<BaseEntity> obj = ((ExtendedDAOBean<BaseEntity>) construct.newInstance());
				tables[i++] = obj.getTableName();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return tables;
	}

	@Override
	public String createBatchQuery() throws IllegalAccessException, InstantiationException, NullPointerException, IllegalArgumentException, InvocationTargetException {
		StringBuffer str = new StringBuffer();
		Constructor<?> construct = null;
		for (Class<? extends ExtendedDAOBean<?>> tableClass : mTableClass) {
			try {
				construct = tableClass.getDeclaredConstructor();
				construct.setAccessible(true);
				ExtendedDAOBean<BaseEntity> obj = ((ExtendedDAOBean<BaseEntity>) construct.newInstance(null));
				str.append(obj.getCreateSyntax());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return str.toString();
	}

	@Override
	public String[] getGetUpdateSyntax(int version) {
		List<String> syntax = new LinkedList<String>();
		Constructor<?> construct = null;
		String command = null;
		for (Class<? extends ExtendedDAOBean<?>> tableClass : mTableClass) {
			try {
				construct = tableClass.getDeclaredConstructor();
				construct.setAccessible(true);
				ExtendedDAOBean<BaseEntity> obj = ((ExtendedDAOBean<BaseEntity>) construct.newInstance());
				command = obj.getChangeSyntax(version);
				if (command != null) {
					syntax.add(command);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return syntax.toArray(new String[syntax.size()]);
	}
}
